#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
int a[3000005];
long long cost[3000005],mod=1e9+7;
long long cnt[3000005],ans1[1000005],ans2[1000005];
int main() {
int T = 1, kase = 0;
cin >> T;
while (T--) {
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i+n]=a[i+n*2]=a[i];
int mx=0,p=-1;
for(int i=n+1;i<=n*2;i++) if(a[i]>mx) mx=a[i],p=i;
cost[p]=cnt[p]=0;
long long c=a[p],s=0;
vector <int> stk;
stk.push_back(p);
for(int i=p-1;i>p-n;i--){
cost[i]=cost[i+1],cnt[i]=cnt[i+1];
if(a[i]<a[stk.back()]) cnt[i]+=a[stk.back()]-a[i];
int lst=0;
while(stk.size()&&a[i]>=a[stk.back()]){
c-=a[stk.back()]-lst,s-=1LL*(a[stk.back()]-lst)*(stk.back()-i-1);
lst=a[stk.back()],stk.pop_back();
}
if(stk.size()){
c-=a[i]-lst,s-=1LL*(a[i]-lst)*(stk.back()-i-1);
}
//cout<<c<<" "<<s<<endl;
cost[i]=(cost[i]+s*2+c)%mod;
s+=c,c+=a[i],stk.push_back(i);
//cout<<cost[i]<<" "<<cnt[i]<<" "<<c<<" "<<s<<endl;
}
stk.clear(),c=a[p],s=0;
stk.push_back(p);
for(int i=p+1;i<p+n;i++){
cost[i]=cost[i-1],cnt[i]=cnt[i-1];
if(a[i]<a[stk.back()]) cnt[i]+=a[stk.back()]-a[i];
int lst=0;
while(stk.size()&&a[i]>=a[stk.back()]){
c-=a[stk.back()]-lst,s-=1LL*(a[stk.back()]-lst)*(i-stk.back()-1);
lst=a[stk.back()],stk.pop_back();
}
if(stk.size()){
c-=a[i]-lst,s-=1LL*(a[i]-lst)*(i-stk.back()-1);
}
//cout<<c<<" "<<s<<endl;
cost[i]=(cost[i]+s*2+c)%mod;
s+=c,c+=a[i],stk.push_back(i);
//cout<<cost[i]<<" "<<cnt[i]<<" "<<c<<" "<<s<<endl;
}
for(int i=p-n+1;i<=p;i++) ans1[(i-1)%n]=cnt[i]+cnt[i+n-1],ans2[(i-1)%n]=(cost[i]+cost[i+n-1])%mod;
for(int i=0;i<n;i++) printf("%lld %lld\n",ans1[i],ans2[i]);
}
return 0;
}
797. All Paths From Source to Target | 1547B - Alphabetical Strings |
1550A - Find The Array | 118B - Present from Lena |
27A - Next Test | 785. Is Graph Bipartite |
90. Subsets II | 1560A - Dislike of Threes |
36. Valid Sudoku | 557. Reverse Words in a String III |
566. Reshape the Matrix | 167. Two Sum II - Input array is sorted |
387. First Unique Character in a String | 383. Ransom Note |
242. Valid Anagram | 141. Linked List Cycle |
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |
112. Path Sum | 1556A - A Variety of Operations |